use actix_web::{get,put,delete,Responder,HttpResponse,web};
use fejpsl_contract::friend::{ MessagerListData, MessagerPageData,FriendMessageListData,FriendMessagePageData};
use fejpsl_contract::banner::{BannerData,BannersData};
use fejpsl_contract::article::{ArticleTopData, ArticleListData, ArticlePageData};
use fejpsl_contract::user::{UserData};
use core::borrow::Borrow;
use fejpsl_contract::wall::{WallListData, WallPageData, WallReaderListData, WallCommentListData, WallCommentPageData};
use fejpsl_contract::rank::{FanListData, FanPageData};
use fejpsl_contract::exam::exam_paper::{PaperListData,PaperPageData};
use fejpsl_contract::exam::single_answer::{SingleAnswerData,SingleAnswerListData};
use fejpsl_contract::exam::multiple_answer::{MultipleAnswerData, MultipleAnswerListData};
use fejpsl_contract::exam::true_false_question::{TrueFalseQuestionData, TrueFalseQuestionListData};
use fejpsl_contract::exam::blank_filling_question::{BlankFillingQuestionData, BlankFillingQuestionListData};
//use std::alloc::System;
//use std::time::SystemTime;

#[path="service.rs"] mod service;

#[get("/")]
async fn index() -> impl Responder {
    format!("Endpoints: /banner ")
}

#[get("/article/rank/json")]
pub async fn top_list_article() -> HttpResponse  {
    let article = service::top_list_article().await;
    let articles_data= ArticleTopData {
        data: article,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(articles_data)
}
#[get("/article/list/{page_no}/json")]
pub async fn get_article_list_by_page_no(info:web::Path<String>) -> HttpResponse  {
    let page_no  = &info.as_str();
    let mut new_string = String::new();
    new_string.push_str(page_no);

    let articles_data = service::get_article_list_by_page_no(&new_string).await;
    //let cur_page:i32=page_no.parse().unwrap();
    let article_list_data = ArticleListData {
        cur_page:0,
        datas:articles_data,
        offset:5,
        over:false,
        page_count:7,
        size:5,
        total:50,
    };
    let article_page_data= ArticlePageData {
        data:article_list_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(article_page_data)
}

#[get("/banner/json")]
pub async fn list_banner() -> HttpResponse  {
    let banner = service::list_banner().await;
//add
    let banners_data= BannersData {
        data: banner,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(banners_data)
}

#[get("/banner/{id}")]
pub async fn get_banner_by_id(info:web::Path<String>) -> HttpResponse  {
    let id  = &info.as_str();
    let mut new_string = String::new();
    new_string.push_str(id);

    let banner = service::get_banner_by_id(&new_string).await;
    let banner_data= BannerData {
        data: banner,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(banner_data)
}

#[delete("/banner/{id}")]
pub async fn delete_banner_by_id(info:web::Path<String>) -> HttpResponse {
    let id  = &info.as_str();
    let mut new_string = String::new();
    new_string.push_str(id);

    let banner = service::delete_banner_by_id(&new_string).await;
    HttpResponse::Ok().json(banner)
}

#[put("/banner/{item_desc}/{image_path}/{is_visible}/{item_order}/{title}/{item_type}/{url}")]
pub async fn insert_banner(info:web::Path<(String, String, i32, i32, String, i32, String)>) -> impl Responder {
    let item_desc  = &info.0;
    let image_path = &info.1;
    let is_visible  = &info.2;
    let item_order = &info.3;
    let title  = &info.4;
    let item_type = &info.5;
    let url  = &info.6;
    let new = service::insert_banner(item_desc,image_path,is_visible,item_order,title,item_type,url).await;
    HttpResponse::Ok().json(new)
}
//friend
#[get("/messager/list/{page_no}/{user_uuid}/json")]
pub async fn messager_list(info:web::Path<(i32,String)>) -> HttpResponse  {
    let page_no  = &info.0;
    let user_uuid = &info.1;
    let messagers_data = service::messager_list(page_no,user_uuid).await;
    let messager_list_data= MessagerListData {
        cur_page:0,
        datas:messagers_data,
        offset:10,
        over:false,
        page_count:7,
        size:10,
        total:50,
    };
    let messager_page_data= MessagerPageData {
        data:messager_list_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(messager_page_data)
}
#[get("/friend_message/list/{page_no}/{send_user_uuid}/{receive_user_uuid}/json")]
pub async fn friend_message_list(info:web::Path<(i32,String,String)>) -> HttpResponse  {
    let page_no  = &info.0;
    let send_user_uuid = &info.1;
    let receive_user_uuid = &info.2;
    let friend_messages_data = service::friend_message_list(page_no,send_user_uuid,receive_user_uuid).await;
    let friend_message_list_data= FriendMessageListData {
        cur_page:0,
        datas:friend_messages_data,
        offset:10,
        over:false,
        page_count:7,
        size:10,
        total:50,
    };
    let friend_message_page_data= FriendMessagePageData {
        data:friend_message_list_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(friend_message_page_data)
}
#[get("/my_follow/list/{page_no}/{user_uuid}/json")]
pub async fn list_my_follow(info:web::Path<(i32,String)>) -> HttpResponse  {
    let page_no  = &info.0;
    let user_uuid = &info.1;
    let follow_page_data = service::list_my_follow(page_no,user_uuid).await;
    HttpResponse::Ok().json(follow_page_data)
}
#[get("/follow_me/list/{page_no}/{user_uuid}/json")]
pub async fn list_follow_me(info:web::Path<(i32,String)>) -> HttpResponse  {
    let page_no=&info.0;
    let user_uuid=&info.1;
    let follow_page_data = service::list_follow_me(page_no,user_uuid).await;
    HttpResponse::Ok().json(follow_page_data)
}
#[get("/block_people/list/{page_no}/{user_uuid}/json")]
pub async fn list_block_people(info:web::Path<(i32,String)>) -> HttpResponse  {
    let page_no=&info.0;
    let user_uuid=&info.1;
    let follow_page_data = service::list_block_people(page_no,user_uuid).await;
    HttpResponse::Ok().json(follow_page_data)
}

#[get("/user/{id}")]
pub async fn get_user_by_id(info:web::Path<String>) -> HttpResponse  {
    let id  = &info.as_str();
    let mut new_string = String::new();
    new_string.push_str(id);

    let user = service::get_user_by_id(&new_string).await;
    let user_data= UserData {
        data: user,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(user_data)
}
#[get("/user_info/{user_uuid}/json")]
pub async fn get_user_by_uuid(info:web::Path<String>) -> HttpResponse  {
    let uuid  = &info.as_str();
    let mut new_string = String::new();
    new_string.push_str(uuid);

    let user = service::get_user_by_uuid(&new_string).await;
    let user_data= UserData {
        data: user,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(user_data)
}
#[put("/user/register/{user_name}/{password}")]
pub async fn user_register(info:web::Path<(String, String)>) -> impl Responder {
    let user_name  = &info.0;
    let password = &info.1;
    let new = service::user_register(user_name,password).await;
    let mut error_code=0;
    let mut error_msg=String::from("");
    let sp=new.borrow();
    if let Some(user)=sp{
        if user.user_uuid.len()>5 {
            error_code=0;
            error_msg="Success.".to_string()
        }else {
            error_code=1;
            error_msg="此用户名已被注册！".to_string()
        }
    }
    let new_data= UserData {
        data: new,
        error_code: error_code,
        error_msg:error_msg,
    };

    HttpResponse::Ok().json(new_data)
}
#[put("/user/login/{user_name}/{password}")]
pub async fn user_login(info:web::Path<(String, String)>) -> impl Responder {
    let user_name  = &info.0;
    let password = &info.1;
    let new = service::user_login(user_name,password).await;
    let mut error_code=0;
    let mut error_msg=String::from("");
    let sp=new.borrow();
    if let Some(user)=sp{
        if user.user_uuid.len()>5 {
            error_code=0;
            error_msg="Success.".to_string()
        }else {
            error_code=1;
            error_msg="用户名称或密码错误！".to_string()
        }
    }
    let new_data= UserData {
        data: new,
        error_code: error_code,
        error_msg:error_msg,
    };

    HttpResponse::Ok().json(new_data)
}
#[put("/user/{id}/{user_name}/{real_name}/{age}/{sex}/{birth_day}/{phone}")]
pub async fn update_user_by_id(info:web::Path<(i32,String, String, i32, i32, String, String)>) -> impl Responder {
    let id  = &info.0;
    let user_name = &info.1;
    let real_name  = &info.2;
    let age = &info.3;
    let sex  = &info.4;
    let birth_day = &info.5;
    let phone  = &info.6;
    let new = service::update_user_by_id(id,user_name,real_name,age,sex,birth_day,phone).await;
    HttpResponse::Ok().json(new)
}
#[put("/user_avatar/{user_uuid}/{avatar_text}")]
pub async fn update_user_avatar(info:web::Path<(String, String)>) -> impl Responder {
    let user_uuid  = &info.0;
    let avatar_text = &info.1;
    let new = service::update_user_avatar(user_uuid,avatar_text).await;
    HttpResponse::Ok().json(new)
}
#[put("/user/phone/{user_uuid}/{phone}")]
pub async fn update_user_phone(info:web::Path<(String, String)>) -> impl Responder {
    let user_uuid  = &info.0;
    let phone = &info.1;
    let new = service::update_user_phone(user_uuid,phone).await;
    HttpResponse::Ok().json(new)
}
#[put("/user/email/{user_uuid}/{email}")]
pub async fn update_user_email(info:web::Path<(String, String)>) -> impl Responder {
    let user_uuid  = &info.0;
    let email = &info.1;
    let new = service::update_user_email(user_uuid,email).await;
    HttpResponse::Ok().json(new)
}
#[put("/user/sex/{user_uuid}/{sex}")]
pub async fn update_user_sex(info:web::Path<(String, i32)>) -> impl Responder {
    let user_uuid  = &info.0;
    let sex = &info.1;
    let new = service::update_user_sex(user_uuid,sex).await;
    HttpResponse::Ok().json(new)
}
#[put("/user/is_explicit/{user_uuid}/{is_explicit}")]
pub async fn update_user_is_explicit(info:web::Path<(String, i32)>) -> impl Responder {
    let user_uuid  = &info.0;
    let is_explicit = &info.1;
    let new = service::update_user_is_explicit(user_uuid,is_explicit).await;
    HttpResponse::Ok().json(new)
}
#[put("/user/birthday/{user_uuid}/{birthday}")]
pub async fn update_user_birthday(info:web::Path<(String, String)>) -> impl Responder {
    let user_uuid  = &info.0;
    let birthday = &info.1;
    let new = service::update_user_birthday(user_uuid,birthday).await;
    HttpResponse::Ok().json(new)
}
#[put("/user/nice_name/{user_uuid}/{nice_name}")]
pub async fn update_user_nice_name(info:web::Path<(String, String)>) -> impl Responder {
    let user_uuid  = &info.0;
    let nice_name = &info.1;
    let new = service::update_user_nice_name(user_uuid,nice_name).await;
    HttpResponse::Ok().json(new)
}
#[put("/user/real_name/{user_uuid}/{real_name}")]
pub async fn update_user_real_name(info:web::Path<(String, String)>) -> impl Responder {
    let user_uuid  = &info.0;
    let real_name = &info.1;
    let new = service::update_user_real_name(user_uuid,real_name).await;
    HttpResponse::Ok().json(new)
}
#[put("/user/area/{user_uuid}/{area}")]
pub async fn update_user_area(info:web::Path<(String, String)>) -> impl Responder {
    let user_uuid  = &info.0;
    let area = &info.1;
    let new = service::update_user_area(user_uuid,area).await;
    HttpResponse::Ok().json(new)
}
#[get("/wall/list/{page_no}/{user_uuid}/json")]
pub async fn get_wall_list_by_page_no(info:web::Path<(i32,String)>) -> HttpResponse  {
    let page_no  = &info.0;
    let user_uuid = &info.1;
    let wall_page_data = service::get_wall_list_by_page_no(page_no,user_uuid).await;
    HttpResponse::Ok().json(wall_page_data)
}
#[get("/wall/reader/list/{wall_id}/json")]
pub async fn get_wall_reader_list_by_wall_id(info:web::Path<i32>) -> HttpResponse  {
    let wall_id  = &info.as_ref();
    let wall_readers_data = service::get_wall_reader_list_by_wall_id(wall_id).await;
    let wall_reader_list_data= WallReaderListData {
        data:wall_readers_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(wall_reader_list_data)
}
#[get("/wall/comment/list/{page_no}/{wall_id}/json")]
pub async fn get_wall_comment_list_by_page_no(info:web::Path<(i32,i32)>) -> HttpResponse  {
    let page_no  = &info.0;
    let wall_id = &info.1;
    let wall_comments_data = service::get_wall_comment_list_by_page_no(page_no,wall_id).await;
    let wall_comment_list_data = WallCommentListData {
        cur_page:0,
        datas:wall_comments_data,
        offset:10,
        over:false,
        page_count:7,
        size:5,
        total:50,
    };
    let wall_comment_page_data= WallCommentPageData {
        data:wall_comment_list_data,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(wall_comment_page_data)
}
// rank
#[get("/rank/fan/list/{page_no}/json")]
pub async fn get_rank_fan_list_by_page_no(info:web::Path<i32>) -> HttpResponse  {
    let page_no  = &info.as_ref();
    let rank_page_data = service::get_rank_fan_list_by_page_no(page_no).await;
    HttpResponse::Ok().json(rank_page_data)
}
#[get("/rank/friend/level_score/list/{page_no}/{user_uuid}/json")]
pub async fn get_rank_friend_level_score_list_by_page_no(info:web::Path<(i32,String)>) -> HttpResponse  {
    let page_no  = &info.0;
    let user_uuid=&info.1;
    let rank_page_data = service::get_rank_friend_level_score_list_by_page_no(page_no,user_uuid).await;
    HttpResponse::Ok().json(rank_page_data)
}
#[get("/rank/world/level_score/list/{page_no}/json")]
pub async fn get_rank_world_level_score_list_by_page_no(info:web::Path<i32>) -> HttpResponse  {
    let page_no  = &info.as_ref();
    let rank_page_data = service::get_rank_world_level_score_list_by_page_no(page_no).await;
    HttpResponse::Ok().json(rank_page_data)
}

// exam
#[get("/exam/paper/list/{page_no}/json")]
pub async fn get_paper_list_by_page_no(info:web::Path<i32>) -> HttpResponse  {
    let page_no  = &info.as_ref();
    let paper_page_data = service::get_paper_list_by_page_no(page_no).await;
    HttpResponse::Ok().json(paper_page_data)
}

// exam category
#[get("/exam/paper/list/{page_no}/{category}json")]
pub async fn get_paper_list_by_category(info:web::Path<(i32,String)>) -> HttpResponse  {
    let page_no  = &info.0;
    let category=&info.1;
    let paper_page_data = service::get_paper_list_by_category(page_no,category).await;
    HttpResponse::Ok().json(paper_page_data)
}
// exam text_content
#[get("/exam/paper/text_content/{subject_id}/json")]
pub async fn get_paper_text_content_by_subject_id(info:web::Path<i32>) -> HttpResponse  {
    let subject_id  = &info.as_ref();
    let text_content = service::get_paper_text_content_by_subject_id(subject_id).await;
    HttpResponse::Ok().json(text_content)
}

//exam
#[get("/exam/paper/single_answer/{id}/json")]
pub async fn get_single_answer_by_id(info:web::Path<i32>) -> HttpResponse  {
    let id  = &info.as_ref();
    let single_answer = service::get_single_answer_by_id(id).await;
    let single_answer_data= SingleAnswerData {
        data: single_answer,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(single_answer_data)
}
#[get("/exam/paper/single_answer/list/{subject_id}/json")]
pub async fn list_single_answer(info:web::Path<i32>) -> HttpResponse  {
    let subject_id  = &info.as_ref();
    let single_answers = service::list_single_answer(subject_id).await;
    let single_answer_list_data= SingleAnswerListData {
        data: single_answers,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };

    HttpResponse::Ok().json(single_answer_list_data)
}
#[get("/exam/paper/multiple_answer/{id}/json")]
pub async fn get_multiple_answer_by_id(info:web::Path<i32>) -> HttpResponse  {
    let id  = &info.as_ref();
    let multiple_answer = service::get_multiple_answer_by_id(id).await;
    let multiple_answer_data= MultipleAnswerData {
        data: multiple_answer,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(multiple_answer_data)
}
#[get("/exam/paper/multiple_answer/list/{subject_id}/json")]
pub async fn list_multiple_answer(info:web::Path<i32>) -> HttpResponse  {
    let subject_id  = &info.as_ref();
    let multiple_answers = service::list_multiple_answer(subject_id).await;
    let multiple_answer_list_data= MultipleAnswerListData {
        data: multiple_answers,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };

    HttpResponse::Ok().json(multiple_answer_list_data)
}
#[get("/exam/paper/true_false_question/{id}/json")]
pub async fn get_true_false_question_by_id(info:web::Path<i32>) -> HttpResponse  {
    let id  = &info.as_ref();
    let true_false_question = service::get_true_false_question_by_id(id).await;
    let true_false_question_data= TrueFalseQuestionData {
        data: true_false_question,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(true_false_question_data)
}
#[get("/exam/paper/true_false_question/list/{subject_id}/json")]
pub async fn list_true_false_question(info:web::Path<i32>) -> HttpResponse  {
    let subject_id  = &info.as_ref();
    let true_false_questions = service::list_true_false_question(subject_id).await;
    let true_false_question_list_data= TrueFalseQuestionListData {
        data: true_false_questions,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };

    HttpResponse::Ok().json(true_false_question_list_data)
}
#[get("/exam/paper/blank_filling_question/{id}/json")]
pub async fn get_blank_filling_question_by_id(info:web::Path<i32>) -> HttpResponse  {
    let id  = &info.as_ref();
    let blank_filling_question = service::get_blank_filling_question_by_id(id).await;
    let blank_filling_question_data= BlankFillingQuestionData {
        data: blank_filling_question,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };
    HttpResponse::Ok().json(blank_filling_question_data)
}
#[get("/exam/paper/blank_filling_question/list/{subject_id}/json")]
pub async fn list_blank_filling_question(info:web::Path<i32>) -> HttpResponse  {
    let subject_id  = &info.as_ref();
    let blank_filling_questions = service::list_blank_filling_question(subject_id).await;
    let blank_filling_question_list_data= BlankFillingQuestionListData {
        data: blank_filling_questions,
        error_code: 0,
        error_msg:"Success.".to_string(),
    };

    HttpResponse::Ok().json(blank_filling_question_list_data)
}
//china
#[get("/api/china")]
pub async fn get_province_list() -> HttpResponse  {
    let province_list = service::get_province_list().await;
    HttpResponse::Ok().json(province_list)
}
#[get("/api/china/{id}")]
pub async fn get_city_list(info:web::Path<String>) -> HttpResponse  {
    let id  = &info.as_str();
    let mut new_string = String::new();
    new_string.push_str(id);
    let city_data = service::get_city_list(&new_string).await;
    HttpResponse::Ok().json(city_data)
}

//community
#[get("/community/my/room/list/{page_no}/{user_uuid}/json")]
pub async fn get_my_room_list_by_page_no(info:web::Path<(i32,String)>) -> HttpResponse  {
    let page_no  = &info.0;
    let user_uuid=&info.1;
    let room_page_data= service::get_my_room_list_by_page_no(page_no,user_uuid).await;
    HttpResponse::Ok().json(room_page_data)
}
#[get("/community/hot/room/list/{page_no}/json")]
pub async fn get_hot_room_list_by_page_no(info:web::Path<i32>) -> HttpResponse  {
    let page_no  = &info.as_ref();
    let room_page_data= service::get_hot_room_list_by_page_no(page_no).await;
    HttpResponse::Ok().json(room_page_data)
}
#[put("/community/create/room/{user_uuid}")]
pub async fn create_room(info:web::Path<String>) -> impl Responder {
    let user_uuid  = &info.as_str();
    let new_data = service::create_room(&user_uuid.to_string()).await;
    HttpResponse::Ok().json(new_data)
}
#[put("/community/join/room/{room_id}/{user_uuid}")]
pub async fn join_room(info:web::Path<(i32,String)>) -> impl Responder {
    let room_id  = &info.0;
    let user_uuid=&info.1;
    let new_data = service::join_room(room_id,user_uuid).await;
    HttpResponse::Ok().json(new_data)
}
#[put("/community/delete/room/{room_id}/{user_uuid}")]
pub async fn delete_room(info:web::Path<(i32,String)>) -> impl Responder {
    let room_id  = &info.0;
    let user_uuid=&info.1;
    let new_data = service::delete_room(room_id,user_uuid).await;
    HttpResponse::Ok().json(new_data)
}
#[get("/community/room/table/list/{room_id}/{page_no}/json")]
pub async fn get_room_table_list_by_page_no(info:web::Path<(i32,i32)>) -> HttpResponse  {
    let room_id  = &info.0;
    let page_no  = &info.1;
    let result_page_data= service::get_room_table_list_by_page_no(room_id,page_no).await;
    HttpResponse::Ok().json(result_page_data)
}
